<?php

/**
 * This is the model class for table "{{transfer_process}}".
 *
 * The followings are the available columns in table '{{transfer_process}}':
 * @property string $id
 * @property integer $transfer_id
 * @property integer $type
 * @property integer $t_index
 * @property string $unique_id
 */
class TransferProcess extends CActiveRecord
{

	const flagNewDiectManager = 1;
	const flagOldDiectManager = 3;
	const flagHrManagers = 2;

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return TransferProcess the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{transfer_process}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('transfer_id, t_index, unique_id', 'required'),
			array('transfer_id, t_index', 'numerical', 'integerOnly'=>true),
			array('unique_id', 'length', 'max'=>32),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, transfer_id, t_index, unique_id ,flag', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'process'=>array(self::BELONGS_TO, 'Transfer', 'transfer_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'transfer_id' => 'Transfer',
			't_index' => 'T Index',
			'unique_id' => 'Unique',
			'flag' => 'Flag'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('transfer_id',$this->transfer_id);
		$criteria->compare('t_index',$this->t_index);
		$criteria->compare('unique_id',$this->unique_id,true);
		$criteria->compare('flag',$this->flag);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}



	/**
	 * @return 流程整理
	 */

	public function formatProcess($o,$n,$hrStart = 0,$artFlag = 0)
	{
		$a = array();
		//print_r($oldProcess);
		if(!empty($o))
		{
			$oldTopDepartmente = $o[count($o)-1]['id'];
		} else {
			return null;
		}
		if(!empty($n))
		{
			//去除空
			foreach ($n as $key => $value)
			{
				if($value['manager_unique_id']=='N/A'||$value['manager_unique_id']=='')
				{
					unset($n[$key]);
				}
			}
			$hrOptionModel = new HrOption;
			//在直接主管后加入 大叔(美术) 小米 璇姐
			$n2 = array();
			//是不是美术
			$i = 2;
			if($artFlag == 1)
			{
				$n2[$i] = array('name' => 'ART DIRECTOR', 'manager_unique_id'=>'C100034');
				$i++;
			}
			//小米
			$hrM = $hrOptionModel->findByAttributes(array('option_name'=>'transfer_michelle'));
			if($hrM) 
			{
				$n2[$i] = array('name' => 'transfer_michelle', 'manager_unique_id'=>$hrM->option_value);
				$i++;
			}
			//array_push($n2, array('name' => 'transfer_michelle', 'manager_unique_id'=>$hrM->option_value));
			//璇姐
			$hrD = $hrOptionModel->findByAttributes(array('option_name'=>'transfer_hr_director'));
			if($hrM) 
			{
				$n2[$i] = array('name' => 'ART DIRECTOR', 'manager_unique_id'=>$hrD->option_value);
			}
			//array_push($n2, array('name' => 'ART DIRECTOR', 'manager_unique_id'=>$hrD->option_value));
			//在n的第一位后插入n2
			foreach($n2 as $key=>$p) 
			{
			  array_splice($n, $key-1, 0, array($p));
			}	
			$newTopDepartmente = $n[count($n)-1]['id'];
			//$n = array();
		} else {
			return null;
		}
		if($oldTopDepartmente == $newTopDepartmente)
		{
			//同一级bu流程
			//两个 部门数组中剔除顶级部门
			array_pop($o);
			array_pop($n);
			//加入这一BU的 hr 专员
			$t = UserDepartment::model()->findByPk($oldTopDepartmente);
			$arr = array('name' => 'D HR', 'manager_unique_id'=>$t->hr_assistant);
			array_push($o , $arr);
		}
		else
		{
			//加入不同BU的 hr 专员
			$to = UserDepartment::model()->findByPk($oldTopDepartmente);
			$tn = UserDepartment::model()->findByPk($newTopDepartmente);
			$arro = array('name' => 'D HR', 'manager_unique_id'=>$to->hr_assistant);
			if($to->hr_assistant)
				array_push($o , $arro);
			$arrn = array('name' => 'D HR', 'manager_unique_id'=>$tn->hr_assistant);
			if($tn->hr_assistant && ($tn->hr_assistant!=$to->hr_assistant))
				array_push($o , $arrn);
		}
		//插入到BU负责人之前
		//$number = (count($n)>1)?2:1;
		//加入33
		$a = array_merge($o,$n);
		$temp = array();
		if(!empty($a))
		{
			foreach ($a as $key => $value) 
			{
				//去除重复的审核人
				if($key == 0)
				{
					$a[$key]['new_direct'] = self::flagOldDiectManager; // 原部门直接主管
				}
				$flag = true;
				if(!in_array($value['manager_unique_id'], $temp)) 
				{
					array_push($temp,$value['manager_unique_id']);
					$flag = false;
				}
				if($value['manager_unique_id']=='N/A' || $value['manager_unique_id']=='' || $flag)
				{
					unset($a[$key]);
					continue;
				}
				if($a[$key+1]['name'] == 'transfer_michelle')
				{
					$a[$key]['new_direct'] = self::flagNewDiectManager; //新部门直接主管
				}
				if($a[$key]['name'] == 'transfer_michelle' ||  $a[$key-1]['name'] == 'transfer_michelle' )
				{
					$a[$key]['new_direct'] = self::flagHrManagers; //璇姐或michelle
				}
			}
			//print_r($a);
			//die;
			return $a;
		}
		return null;
	}


	public function saveProcess($transferId , $p)
	{
		if(!empty($p))
		{
			$i = 0;
			foreach ($p as $value)
			{
				$model = new TransferProcess;
				$i++;
				$model->unique_id = $value['manager_unique_id'];
				$model->t_index = $i;
				$model->transfer_id = $transferId;
				$model->flag = $value['new_direct'] != 0 ?  $value['new_direct'] : 0;
				$model->save(false);
			}
			return true;
		}
	}

	//获取新部门的直接主管
	public function getUserNewDepartmentDirectManager($toDepartmentId)
	{
		$n = UserDepartment::model()->getPath($toDepartmentId);
		return $n[count($n)-1]['manager_unique_id'];
	}
}